<?php
/**
 * @deprecated
 * @author Akulov Denis <akulov.d.g@gmail.com>
 * @since 09.07.2011
 */
class Hook_View implements Hook_View_Interface {

    private $_arHooks = array();


    private $_arHooksValues = array();


    /**
     * @var Config_Interface
     */
    private $_objConfig = null;


    public function __construct (Config_Interface $objConfig) {
        $this->_objConfig = $objConfig;
    }


    private function getHookDirectory () {
        return $this->_objConfig->getAttribute('FW_PATHS', 'PROTOTYPE');
    }


    private function getHookValues ($strHookName) {
        return $this->_arHooksValues[$strHookName];
    }


    private function getHooks () {
        return $this->_arHooks;
    }


    public function addHook ($strHookName, $strHookFile) {
        if (empty($strHookName) || empty($strHookFile)) {
            throw new Exception('Incomplite hook description name: {'.$strHookName.'} file: {'.$strHookFile.'}');
        }


        if (!empty($this->_arHooks[$strHookName])) {
            throw new Exception('Hooks in conflict: '.$strHookName);
        }

        $this->_arHooks[$strHookName] = $strHookFile;
    }


    public function addValue($strHookName, $strValueName, $mixValue) {
        if (!array_key_exists($strHookName, $this->getHooks())) {
            throw new Exception('Attempt set values in non existing hook: hook name: {'.
                $strHookName.'} value name: {'.$strValueName.'}');
        }

        $this->_arHooksValues[$strHookName][$strValueName] = $mixValue;
    }


    public function render ($strContent) {
        foreach ($this->getHooks() as $strHookName => $strHookFile) {
            $strTmpHookPattern = '<!--x:'.$strHookName.' -->';
            $strTmpHookContent = $this->_render($strHookName, $strHookFile).$strTmpHookPattern;
            $strContent = str_replace($strTmpHookPattern, $strTmpHookContent, $strContent);
        }

        return $strContent;
    }



    private function _render($strHookName, $strHookFile) {
        $strPathToHook =  $this->getHookDirectory();
        $strPathToHook .= 'MVC/V/Hooks/';
        $strPathToHook .= str_replace('_', '/', $strHookFile).'.php';

        foreach ($this->getHookValues($strHookName) as $strValueName => $mixValue) {
            $$strValueName = $mixValue;
        }

        $strHookContent = '';

        ob_start();
        require $strPathToHook;
        $strHookContent = ob_get_contents();
        ob_end_clean();

        return $strHookContent;
    }
}